package me.uniauto.util;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by hardy on 2018/2/22.
 * 格式化返回值的参数
 */
public class JsonDateValueProcessor implements JsonValueProcessor {

    String format = "yyyy-MM-dd HH:mm:ss";


    public JsonDateValueProcessor(){
        super();
    }

    public JsonDateValueProcessor(String format){
        super();
        this.format = format;
    }

    @Override
    public Object processArrayValue(Object o, JsonConfig jsonConfig) {
        return process(o);
    }

    @Override
    public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
        return process(o);
    }


    private Object process(Object obj) {
        if (obj instanceof Date) {
            SimpleDateFormat sf = new SimpleDateFormat(format, Locale.CHINA);
            return sf.format(obj);
        }
        if (obj instanceof BigDecimal) {
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(obj);
        }
        return obj == null ? "" : obj.toString();
    }
}
